//
//  ViewController.m
//  block回调
//
//  Created by 李伟 on 16/1/23.
//  Copyright © 2016年 李伟. All rights reserved.
//

#import "ViewController.h"
#import "Tools.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *passWord;


@property (weak, nonatomic) IBOutlet UILabel *lb_userName;
@property (weak, nonatomic) IBOutlet UILabel *lb_passWord;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 将数据写入沙盒中
- (IBAction)storeAccount:(UIButton *)sender {
    
    NSString *userName = self.userName.text;
    NSString *passWord = self.passWord.text;
    
    NSDictionary *account = @{@"userName":userName,@"passWord":passWord};
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *filePath = [path stringByAppendingPathComponent:@"account.data"];
    
    // 写入数据
    [account writeToFile:filePath atomically:YES];
    
    NSLog(@"写入数据%@",account);
    
    [self.passWord resignFirstResponder];
    
    self.userName.text = nil;
    self.passWord.text = nil;
    
}

/// 通过block回调,将读取的数据传过来
- (IBAction)showAccount:(UIButton *)sender {
    
    [Tools loadAccount:^(NSDictionary *account) {
        
        self.lb_userName.text = account[@"userName"];
        self.lb_passWord.text = account[@"passWord"];
        
    }];
    
}

@end
